package com.dai.spec.gxk.controller;

import com.dai.spec.common.annotation.Log;
import com.dai.spec.common.core.controller.BaseController;
import com.dai.spec.common.core.domain.AjaxResult;
import com.dai.spec.common.core.page.TableDataInfo;
import com.dai.spec.common.enums.BusinessType;
import com.dai.spec.common.enums.SexType;
import com.dai.spec.common.exception.base.BaseException;
import com.dai.spec.common.utils.poi.ExcelUtil;
import com.dai.spec.gxk.domain.Student;
import com.dai.spec.gxk.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生信息管理Controller
 *
 * @author daikl
 * @date 2024-02-29
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/gxk/student")
@Api(tags = "学生管理")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;

    /**
     * 查询学生信息管理列表
     */
    @ApiOperation("查询学生列表")
    @PreAuthorize("@ss.hasPermi('gxk:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student) {
        startPage();
        List<Student> list = studentService.queryList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生信息管理列表
     */
    @ApiOperation("导出学生列表")
    @PreAuthorize("@ss.hasPermi('gxk:student:export')")
    @Log(title = "学生信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student) {
        List<Student> list = studentService.queryList(student);
        List<Student> coverList = coverStudent(list);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, coverList, "学生信息管理数据");
    }

    private static List<Student> coverStudent(List<Student> list) {
        List<Student> coverList = list.stream().map(item -> {
            item.setSex(SexType.getValueByCode(item.getSex()));
            return item;
        }).collect(Collectors.toList());
        return coverList;
    }

    /**
     * 获取学生信息管理详细信息
     */
    @ApiOperation("获取学生详细信息")
    @PreAuthorize("@ss.hasPermi('gxk:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(studentService.getById(id));
    }

    /**
     * 新增学生信息管理
     */
    @ApiOperation("新增学生")
    @PreAuthorize("@ss.hasPermi('gxk:student:add')")
    @Log(title = "学生信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student) {
        this.validateStudent(student);
        //新增学生，公选课总分默认为0
        student.setCourseCredit(new BigDecimal(0));
        return toAjax(studentService.saveWithUser(student));
    }



    /**
     * 修改学生信息管理
     */
    @ApiOperation("修改学生")
    @PreAuthorize("@ss.hasPermi('gxk:student:edit')")
    @Log(title = "学生信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student) {
        return toAjax(studentService.updateWithUser(student));
    }

    /**
     * 删除学生信息管理
     */
    @ApiOperation("删除学生")
    @PreAuthorize("@ss.hasPermi('gxk:student:remove')")
    @Log(title = "学生信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(studentService.removeByIdsWithUser(Arrays.asList(ids)));
    }

    /**
     * 校验
     * @param student
     */
    private static void validateStudent(Student student) {
        if (null == student){
            throw new BaseException("学生对象不能为空！");
        }
    }
}
